package com.liyiqing.secondhandmall.memberservice.service.impl;

import com.liyiqing.commonservice.utils.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.liyiqing.secondhandmall.memberservice.dao.MemberUserDao;
import com.liyiqing.secondhandmall.memberservice.entity.MemberUserEntity;
import com.liyiqing.secondhandmall.memberservice.service.MemberUserService;

import javax.annotation.Resource;


@Service("memberUserService")
public class MemberUserServiceImpl extends ServiceImpl<MemberUserDao, MemberUserEntity> implements MemberUserService {

    @Resource
    private MemberUserDao memberUserDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberUserEntity> page = this.page(
                new Query<MemberUserEntity>().getPage(params),
                new QueryWrapper<MemberUserEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public R insertMemberUser(MemberUserEntity memberUser) {
        //首先根据账号去数据库查找是否存在重复账号
        MemberUserEntity u = memberUserDao.selectUserByAccount(memberUser.getAccount());
        //如果当前账号重复
        if (u != null){
            return R.error(StatusCode.VALID_EXCEPTION.getCode(),"注册失败，'"+memberUser.getAccount() +"' 账号已存在！");
        }
        //否则执行插入操作
        else {
            if (memberUser.getStatus()==null){
                memberUser.setStatus(1);
            }
            memberUser.setCreateTime(NowTime.getNowTime());
            //执行数据的插入操作
            int  n = memberUserDao.insert(memberUser);
            return R.ok().put("data",memberUser);
        }
    }


    @Override
    public R selectAllMemberUser() {
        List<MemberUserEntity> list = memberUserDao.selectList(null);
        if (list == null || list.size()==0){
            return R.error(StatusCode.DATA_NOT_FOUND.getCode(),StatusCode.DATA_NOT_FOUND.getMsg());
        }
        return R.ok().put("data", list);
    }


    @Override
    public R selectUserById(Long id) {
        MemberUserEntity memberUserEntity =  memberUserDao.selectUserById(id);
        if (memberUserEntity == null){
            return R.error(StatusCode.DATA_NOT_FOUND.getCode(),StatusCode.DATA_NOT_FOUND.getMsg());
        }
        return R.ok().put("data", memberUserEntity);
    }


    @Override
    public R updateUserById(MemberUserEntity memberUser) {
        Long id = memberUser.getId();
        if (id==null || id == 0){
            return R.error(StatusCode.VALID_EXCEPTION.getCode(),"修改信息时，账号id不能为空");
        }
        //首先需要判断，想要更新的新账号是否已经存在
        MemberUserEntity user1 = memberUserDao.selectUserByAccount(memberUser.getAccount());
        //如果当前账号存在，且id与当前需要更新的id号不同，即表示有两个id不同但account却相同的账号
        if (user1 != null && user1.getId() != id){
            return R.error(StatusCode.VALID_EXCEPTION.getCode(), memberUser.getAccount() +" 账号已存在，更新失败！");
        }
        //如果不存在重复的account
        user1 = memberUserDao.selectUserById(id);//根据前端给的id去数据库查找对应的对象
        //如果前端传来对象的某个属性值为空，就把数据库中的原值存进去
        if (memberUser.getAccount()==null || "".equals(memberUser.getAccount())){
            memberUser.setAccount(user1.getAccount());
        }
        if (memberUser.getUsername()==null || "".equals(memberUser.getUsername())){
            memberUser.setUsername(user1.getUsername());
        }
        if (memberUser.getPassword()==null|| "".equals(memberUser.getPassword())){
            memberUser.setPassword(user1.getPassword());
        }
        if (memberUser.getMobile()==null|| "".equals(memberUser.getMobile())){
            memberUser.setMobile(user1.getMobile());
        }
        if (memberUser.getWechat()==null|| "".equals(memberUser.getWechat())){
            memberUser.setWechat(user1.getWechat());
        }
        if (memberUser.getQq()==null|| "".equals(memberUser.getQq())){
            memberUser.setQq(user1.getQq());
        }
        if (memberUser.getEmail()==null|| "".equals(memberUser.getEmail())){
            memberUser.setEmail(user1.getEmail());
        }
        if (memberUser.getHeader()==null|| "".equals(memberUser.getHeader())){
            memberUser.setHeader(user1.getHeader());
        }
        if (memberUser.getGender()==null ){
            memberUser.setGender(user1.getGender());
        }
        if (memberUser.getBirth()==null){
            memberUser.setBirth(user1.getBirth());
        }
        if (memberUser.getCity()==null|| "".equals(memberUser.getCity())){
            memberUser.setCity(user1.getCity());
        }
        if (memberUser.getRole()==null){
            memberUser.setRole(user1.getRole());
        }
        if (memberUser.getCreateTime()==null){
            memberUser.setCreateTime(user1.getCreateTime());
        }
        if (memberUser.getStatus()==null ){
            memberUser.setStatus(user1.getStatus());
        }

        //执行真正的更新操作
        int n = memberUserDao.updateById(memberUser);
        if (n>0){
            return R.ok().put("data",memberUser);
        }
        else {
            return R.error(StatusCode.VALID_EXCEPTION.getCode(),"数据库提示：更新失败！");
        }

    }




}